﻿<?php

define ("DEBUG_MODE", false);


// $search = catchEntitiesFromLocationExceptOpponent("眼镜", 39.915, 116.404, 2000, "亮视点");
// var_dump($search->results);
	
// echo catchCityFromLabel("湖南省长沙市芙蓉市工")."1<br>";
// echo catchCityFromLabel("湖南省长沙市")."2<br>";
// echo catchCityFromLabel("中国上海市")."2<br>";
// echo catchCityFromLabel("长沙市")."3<br>";
// echo catchCityFromLabel("长沙")."4<br>";
// echo catchCityFromLabel("香港")."5<br>";
// echo catchCityFromLabel("香港九龙")."6<br>";
// echo catchCityFromObject(32,116, "长沙市芙蓉区")."2<br>";
// echo catchCityFromObject(32,116, "")."3<br>";
// echo catchCityFromObject(21,115, "澳门市")."4<br>";
// echo catchCityFromObject(21,115, "澳门")."5<br>";
// echo catchCityFromObject(21,115, "香港九龙")."4<br>";
// echo catchCityFromObject(22.53,113.94, "")."6<br>";

// if (preg_match("/^[\x{4e00}-\x{9fa5}]+$/u",catchCityFromObject(22.53,113.94, "")))
    // echo "get";

function catchCityFromObject($x, $y, $label)
{
    $city = catchCityFromLabel($label);
    if ($city == ""){
        $city = catchCityFromLocation($x, $y);
    }
    return $city;
}

function catchCityFromLabel($longAddress)
{
    //一个汉字占3个位置！！！
    // echo stripos($longAddress, "省")/3;
    // echo stripos($longAddress, "市")/3;
	if (strpos($longAddress, "香港") !== false){
		return "香港";
	}else if (strpos($longAddress, "澳门") !== false){
		return "澳门";
	}

    $sPosition = (strpos($longAddress, "省"))?stripos($longAddress, "省"):stripos($longAddress, "国");
    $cPosition = stripos($longAddress, "市");
    if ($sPosition > 0){
        return substr($longAddress, $sPosition + 3, $cPosition - $sPosition - 3);
    }else if ($cPosition > 0){
        return substr($longAddress, 0, $cPosition);
    }else{
        return "";
    }
}
function catchCityFromLocation($x, $y)
{   
    $mapObj = new baiduMapClient();
	$result = $mapObj->Geocoding_coordinate_address($x.",".$y);
    $city = strtr($result->result->addressComponent->city, array('市'=>''));
    if (!empty($city)){
        return $city;
    }else{
        return $result->result->addressComponent->province;
    }
}

function catchEntitiesFromLocation($entity, $x, $y, $radius)
{   
    $mapObj = new baiduMapClient();
	$search = $mapObj->Place_search($entity, $x.",".$y, $radius);
	$results = $search->results;
	$shopArray = array(); 
	for ($i = 0; $i < 8; $i++) {
		//if (strstr($results[$i]->name,"亮视点") !== false){continue;}
        $shopArray[] =  array(
            "title" =>  "【".$results[$i]->name."】".$results[$i]->address.(isset($results[$i]->telephone)?" 电话:".$results[$i]->telephone:""),
            "description" =>  "",
            "pic" =>  "",
            "url" =>  "",
        );
    }
	return $shopArray;
}

function catchEntitiesFromLocationExceptOpponent($entity, $x, $y, $radius, $opponent)
{   
    $mapObj = new baiduMapClient();
	$search = $mapObj->Place_search($entity, $x.",".$y, $radius);
	$results = $search->results;
	$shopArray = array(); 
	for ($i = 0; $i < 7; $i++) {
		if (strstr($results[$i]->name, $opponent) !== false){continue;}
        $shopArray[] =  array(
            "title" =>  "【".$results[$i]->name."】".$results[$i]->address.(isset($results[$i]->telephone)?" 电话:".$results[$i]->telephone:""),
            "description" =>  "",
            "pic" =>  "http://discuz.comli.com/weixin/english/pictures/glasses.jpg",
            "url" =>  "",
        );
    }
	return $shopArray;
}


class baiduMapClient
{	
	private $api_server_url;
	private $auth_params;

	public function __construct()
	{
		$this->api_server_url = "http://api.map.baidu.com/";
    	$this->auth_params = array();
   		$this->auth_params['key'] = "401f9a693dd267dd9a4661ec0895fb20";
        $this->auth_params['output'] = "json";
	}

	public function Geocoding_coordinate_address($location) 
	{   
		return $this->call("geocoder", array("location" => $location));
	}
    
    //http://api.map.baidu.com/place/search?&query=%E9%93%B6%E8%A1%8C&location=39.915,116.404&radius=2000&output=xml&key=37492c0ee6f924cb5e934fa08c6b1676
	public function Place_search($query, $location, $radius) 
	{
		return $this->call("place/search", array("query" => $query, "location" => $location, "radius" => $radius));
	}
    
    protected function call($method, $params = array())
    {
		$headers = array(
			"User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:14.0) Gecko/20100101 Firefox/14.0.1",
			"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
			"Accept-Language: en-us,en;q=0.5",
			//"Accept-Encoding: gzip, deflate",
			"Referer: http://developer.baidu.com/"
		);
    	$params = array_merge($this->auth_params, $params);
		$url = $this->api_server_url . "$method?".http_build_query($params);
		if (DEBUG_MODE){echo "REQUEST: $url" . "\n";}
    	$ch = curl_init();
    	curl_setopt($ch, CURLOPT_URL, $url);
    	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		//curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
     	$data = curl_exec($ch);
    	curl_close($ch);    
		$result = null;
		if (!empty($data)){
			if (DEBUG_MODE){
				echo "RETURN: " . $data . "\n";
			}
			$result = json_decode($data);
		}
        else{
            echo "cURL Error:". curl_error($ch);
        }
		return $result;
    }
}
?>

